﻿using ConfExpert.Forms.Base;
using ConfExpert.Properties;

using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Depo
{
    public partial class Kalite2 : XForm
    {
        public Kalite2()
        {
            InitializeComponent();
        }

        private void Kalite2_Load(object sender, EventArgs e)
        {
            
            barCheckItem1.CheckedChanged += barCheckItem1_CheckedChanged;
            bbiKaydet.Visibility =ytk(110) ? BarItemVisibility.Always : BarItemVisibility.Never;
            bbiKaydet.ItemClick += bbiKaydet_ItemClick;

            gvTop.Init("GetKalite2", barCheckItem1.Checked, WhoAmI);
            gvTop.DataTable().PrimaryKey = new DataColumn[] { gvTop.DataTable().Columns["RollId"] };
            GridManager gm2 = GridManager.GetManager(gvTop);
            gm2.GridMenu.AddMenu("Seçili topları sepete ekle", Sec_Top, Resources.Plus_16x16);
            gm2.GridMenu.AddMenu("Seçili topları sepetten çıkar", Birak_Top, Resources.Minus_16x16);
            gm2.GridMenu.AddMenu("Top Hareketleri", ViewRollHistory, Resources.history_16);

            gvTop.MakeReadOnly("Kalite2", "KaliteKontrolId");
            gvTop.YanYanaGetirSoluna("ShelfId", "Kalite2", "KaliteKontrolId");
            gvTop.MakeColumnInvisible("Product");
            gvTop.FormatAsNumber(1, "Qty", "InitialQty");
            gvTop.FormatAsDate("GelisTarihi");
            gvTop.AddSummary("Qty", "InitialQty");
            gvTop.BestFitColumns();
            gvTop.Columns["Kalite2"].Width = 200;

            gvTop.OptionsView.EnableAppearanceOddRow =
            gvTop.OptionsView.EnableAppearanceEvenRow = false;  

            gvTop.RowStyle += new RowStyleEventHandler(gvTop_RowStyle);

        }

        void bbiKaydet_ItemClick(object sender, ItemClickEventArgs e)
        {
            ExecuteNonQuery("UpdKaliteKontrol_Top", gvTop.SerializeChanges());
            gvTop.BestFitColumns();
        }

        void barCheckItem1_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvTop.RefreshDataSource(true, "GetKalite2", barCheckItem1.Checked, WhoAmI);
            gvTop.BestFitColumns();
            gvTop.Columns["Kalite2"].Width = 200;
        }


        void Sec_Top(object sender, EventArgs e)
        {
            bool NA = false;
            foreach (int handle in gvTop.GetSelectedRows())
                if (intParse(gvTop.GetRowCellValue(handle, "IDLE")) <= 2 && (gvTop.GetRowCellValue(handle, "DepoId").ToInt() != 4 ||ytk(111)))
                {
                    int RollId = intParse(gvTop.GetRowCellValue(handle, "RollId"));
                    gvTop.DataTable().Rows.Find(RollId)["QtySelected"] = gvTop.DataTable().Rows.Find(RollId)["Qty"];
                }
                else
                    NA = true;

            UpdSepet();

            if (NA)
                XtraMessageBox.Show("Seçtiğiniz toplardan askıda olanlar (ONAY_BEKLEYEN, ÇIKILACAK veya KALİTE_KONTROL_BEKLEYEN) ve DU4/DESTEX rafındakiler sepete atılamaz.");
        }


        void ViewRollHistory(object sender, EventArgs e)
        {
            int RollId = gvTop.GetRowCellValue(gvTop.FocusedRowHandle, "RollId").ToInt();
            using (TopGecmisi tg = new TopGecmisi(RollId))
                tg.ShowDialog();
        }

        void Birak_Top(object sender, EventArgs e)
        {
            foreach (int handle in gvTop.GetSelectedRows())
            {
                int RollId = intParse(gvTop.GetRowCellValue(handle, "RollId"));
                gvTop.DataTable().Rows.Find(RollId)["QtySelected"] = 0;
                //ds.xStkLines.FindByRollId(RollId).QtySelected = 0;
            }
            UpdSepet();
        }


        private void UpdSepet()
        {
            DataTable dt = new DataTable("Ozet");
            dt.Columns.AddRange(new DataColumn[] {
                new DataColumn("RollId", typeof(int)),
                new DataColumn("QtySelected", typeof(decimal))
            });

            string chg = gvTop.SerializeChanges();
            if (chg != string.Empty)
                ExecuteNonQuery("UpdSepet2", WhoAmI, chg);

            gvTop.DataTable().AcceptChanges();
            gvTop.RefreshData();
        }


        void gvTop_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (decimalParse(((GridView)sender).GetRowCellValue(e.RowHandle, "QtySelected")) > 0)
                e.Appearance.BackColor = Color.LightBlue;
        }
    }
}
